package com.github.prontera.job;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import com.github.prontera.SpringUtils;
import com.github.prontera.domain.ScheduleJob;
import com.github.prontera.domain.ScheduleJobHistory;
import com.github.prontera.domain.enums.TaskStatusEnum;
import com.github.prontera.persistence.ScheduleJobHistoryMapper;

public class TaskUtils {

	public final static Logger log = LoggerFactory.getLogger(TaskUtils.class);

	/**
	 * 通过反射调用scheduleJob中定义的方法
	 */
	public static void invokMethod(ScheduleJob scheduleJob) {
		String msString = "执行失败,反射执行错误";
		boolean hasError = false;
		Object object = null;
		Class<?> clazz = null;
		if (!StringUtils.isEmpty(scheduleJob.getClassName())) {
			try {
				clazz = Class.forName(scheduleJob.getClassName());
				object = clazz.newInstance();
			} catch (Exception e) {
				hasError = true;
				e.printStackTrace();
				log.error("EROOR,队列[{}]中[{}]任务. [{}]", scheduleJob.getJobGroup(), scheduleJob.getJobName(), "启动失败，类名(class)设置错误");
			}
		}
		if (object == null) {
			msString = "请检查是否配置正确";
			log.error("EROOR,队列[{}]中[{}]任务. [{}]", scheduleJob.getJobGroup(), scheduleJob.getJobName(), "启动失败，类名(class)设置错误");
			return;
		}
		clazz = object.getClass();
		Method method = null;
		try {
			method = clazz.getDeclaredMethod(scheduleJob.getMethodName(), ScheduleJob.class);
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
			hasError = true;
			log.error("EROOR,队列[{}]中[{}]任务. [{}]", scheduleJob.getJobGroup(), scheduleJob.getJobName(), "启动失败，方法名(method)设置错误");
		} catch (SecurityException e) {
			hasError = true;
			e.printStackTrace();
			log.error("EROOR,队列[{}]中[{}]任务. [{}]", scheduleJob.getJobGroup(), scheduleJob.getJobName(), "启动失败，方法名(method)设置错误");
		}
		if (!hasError && method != null) {
			try {
				method.invoke(object, scheduleJob);
				msString = "执行成功";
				log.debug("执行成功队列[{}]中[{}]任务.", scheduleJob.getJobGroup(), scheduleJob.getJobName());
				ScheduleJobHistoryMapper scheduleJobHistoryMapper = SpringUtils.getBean(ScheduleJobHistoryMapper.class);
				ScheduleJobHistory scheduleJobHistory = new ScheduleJobHistory();
				scheduleJobHistory.setDesc(msString);
				scheduleJobHistory.setStatus(TaskStatusEnum.BLOCKED);
				scheduleJobHistory.setGroubName(scheduleJob.getJobGroup());
				scheduleJobHistory.setJobName(scheduleJob.getJobName());
				scheduleJobHistoryMapper.insert(scheduleJobHistory);
			} catch (IllegalAccessException e) {
				log.error("EROOR,队列[{}]中[{}]任务. [{}]", scheduleJob.getJobGroup(), scheduleJob.getJobName(), "执行失败，请设置正确的参数");
				hasError = true;
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				log.error("EROOR,队列[{}]中[{}]任务. [{}]", scheduleJob.getJobGroup(), scheduleJob.getJobName(), "执行失败，请设置正确的参数");
				hasError = true;
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				log.error("EROOR,队列[{}]中[{}]任务. [{}]", scheduleJob.getJobGroup(), scheduleJob.getJobName(), "执行失败，请设置正确的参数");
				hasError = true;
				e.printStackTrace();
			} catch (Exception e) {
				log.error("EROOR,队列[{}]中[{}]任务. [{}]", scheduleJob.getJobGroup(), scheduleJob.getJobName(), "执行失败，请设置正确的参数");
				hasError = true;
				e.printStackTrace();
			}
		}
		if (hasError) {
			ScheduleJobHistoryMapper scheduleJobHistoryMapper = SpringUtils.getBean(ScheduleJobHistoryMapper.class);
			ScheduleJobHistory scheduleJobHistory = new ScheduleJobHistory();
			scheduleJobHistory.setDesc(msString);
			scheduleJobHistory.setStatus(TaskStatusEnum.ERROR);
			scheduleJobHistory.setGroubName(scheduleJob.getJobGroup());
			scheduleJobHistory.setJobName(scheduleJob.getJobName());
			scheduleJobHistoryMapper.insert(scheduleJobHistory);
		}
	}
}
